package com.example.xyr.myapplication.adapter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xyr.myapplication.R;
import com.example.xyr.myapplication.base.RVBaseAdapter;
import com.example.xyr.myapplication.base.RVBaseHolder;
import com.example.xyr.myapplication.model.BaseFriendBean;
import com.example.xyr.myapplication.myinterface.FOnItemClickListener;
import com.example.xyr.myapplication.view.CustomImageView;
import com.example.xyr.myapplication.view.RedLabel;

import butterknife.BindView;
import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;

/**
 * Created by ylcf on 2018/8/3.
 */

public class FriendAdapter extends RVBaseAdapter<BaseFriendBean, RVBaseHolder> {

    private Activity mActivity;

    private FOnItemClickListener<BaseFriendBean> listener;

    public FriendAdapter(Activity activity) {
        this.mActivity = activity;
    }

    @Override
    public RVBaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RVBaseHolder holder = null;
        switch (viewType) {
            case ChatType.TYPE_FRIEND_HEAD:
                View friendView = LayoutInflater.from(parent.getContext()).inflate(R.layout.friend_head_item, parent, false);
                holder = new FriendHead(friendView);
                break;

            case ChatType.FRIEND_TYPE:
                View userView = LayoutInflater.from(parent.getContext()).inflate(R.layout.friend_user_item, parent, false);
                holder = new FriendUser(userView);
                break;
            case ChatType.TYPE_NULL:
                break;
        }
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        BaseFriendBean base = mDatas.get(position);
        if (base.getFriendType() == ChatType.TYPE_FRIEND_HEAD) {
            return ChatType.TYPE_FRIEND_HEAD;
        } else if (base.getFriendType() == ChatType.FRIEND_TYPE) {
            return ChatType.FRIEND_TYPE;
        } else {
            return ChatType.TYPE_NULL;
        }
    }

    class FriendHead extends RVBaseHolder {
        @BindView(R.id.friend_item_icon)
        CustomImageView icon;
        @BindView(R.id.friend_item_tv)
        TextView tvName;
        @BindView(R.id.friend_item)
        RelativeLayout item;

        public FriendHead(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(RVBaseHolder holder, final int adapterPosition) {
            final BaseFriendBean baseFriendBean = mDatas.get(adapterPosition);
            icon.setImageResource(baseFriendBean.getIcon());
            tvName.setText(baseFriendBean.getName());
            if (listener != null && listener instanceof FOnItemClickListener) {
                item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.onClick(adapterPosition, baseFriendBean);
                    }
                });
            }
            if (adapterPosition == 2) {
                RedLabel label3 = new RedLabel(mActivity, 20);
                label3.setNum(30);
                label3.setTargetView(tvName);
            }
        }
    }

    class FriendUser extends RVBaseHolder {
        @BindView(R.id.tv_item_pinyin)
        TextView tvItemPinyin;
        @BindView(R.id.civ_item_avatar)
        CustomImageView civItemAvatar;
        @BindView(R.id.tv_item_name)
        TextView tvItemName;
        @BindView(R.id.lly_user_info)
        LinearLayout llyUser;

        public FriendUser(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(RVBaseHolder holder, final int adapterPosition) {
            final BaseFriendBean bean = mDatas.get(adapterPosition);
            // 获取首字母的assii值
            char selection = bean.getPiyin().charAt(0);
            // 通过首字母的assii值来判断是否显示字母
            int positionForSelection = getPositionForSelection(selection);
            if (adapterPosition == positionForSelection) {// 相等说明需要显示字母
                tvItemPinyin.setVisibility(View.VISIBLE);
                tvItemPinyin.setText(String.valueOf(selection));
            } else {
                tvItemPinyin.setVisibility(View.GONE);

            }
            tvItemName.setText(bean.getUserInfo().getNickname());

            bean.getUserInfo().getAvatarBitmap(new GetAvatarBitmapCallback() {
                @Override
                public void gotResult(int i, String s, Bitmap bitmap) {
                    if (i == 0) {
                        civItemAvatar.setImageBitmap(bitmap);
                    }
                }
            });

            if (listener != null) {
                llyUser.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.onClick(adapterPosition, bean);
                    }
                });
            }
        }
    }

    public void setOnItemClickListener(FOnItemClickListener listener) {
        this.listener = listener;
    }


    public int getPositionForSelection(char selection) {
        for (int i = 0; i < mDatas.size(); i++) {
            BaseFriendBean bean = mDatas.get(i);
            if (bean.getFriendType() == ChatType.FRIEND_TYPE) {
                char first = bean.getPiyin().charAt(0);
                if (first == selection) {
                    return i;
                }
            }
        }
        return -1;
    }


}
